package com.ruoyi.web.controller.system;


import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.ResponseResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BusiException;
import com.ruoyi.common.utils.GoogleAuthenticationTool;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.biz.CashBizService;
import com.ruoyi.system.domain.Cash;
import com.ruoyi.system.domain.CashHistory;
import com.ruoyi.system.dto.CashAddDto;
import com.ruoyi.system.service.CashHistoryService;
import com.ruoyi.system.service.CashService;
import com.ruoyi.web.controller.common.CheckTimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

@Api(tags = "管理后台:钱包相关接口")
@RestController
@RequestMapping("/admin/cash")
@Slf4j
public class AdminCashController extends AdminController {
    @Resource
    private CashService cashService;

    @Resource
    private CashBizService cashBizService;

    @Resource
    private CashHistoryService cashHistoryService;


    @ApiOperation(value = "钱包详情")
    @PostMapping("info")
    public ResponseResult<Cash> info() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Cash cash = cashService.getByUserId(loginUser.getUserId());
        return this.setSuccessResult(cash);
    }

    @ApiOperation(value = "余额列表")
    @GetMapping("list")
    public ResponseResult<List<Cash>> list(Cash Cash) {
        this.checkAdmin();
        this.startPage();
        List<Cash> list = cashService.findList(Cash);
        return this.setSuccessResult(this.getPageResult(list));
    }

    @ApiOperation(value = "余额明细")
    @GetMapping("listHis")
    public ResponseResult<List<CashHistory>> listHis(CashHistory cashHistory) {
        SysUser user = this.getLoginUser().getUser();
        if (!StrUtil.equals(user.getRole(), "admin")) {
            cashHistory.setUserId(user.getUserId());
        }
        this.startPage();
        List<CashHistory> list = cashHistoryService.findList(cashHistory);
        return this.setSuccessResult(this.getPageResult(list));
    }

    @ApiOperation(value = "查看数据")
    @PostMapping("scan")
    public ResponseResult<List<CashHistory>> update(@RequestBody CashHistory cashHistory) {

        List<CashHistory> list = cashHistoryService.list(cashHistory);
        for (int i = 0; i < list.size(); i++) {
            CashHistory first = list.get(i);
            if (i == 0) {
                continue;
            }
            if (i == list.size() - 1) {
                break;
            }
            CashHistory second = list.get(i + 1);
            BigDecimal changeMoney = second.getChangeMoney();
            if (StrUtil.equals(second.getChangeType(), "reduce")) {
                changeMoney = BigDecimal.ZERO.subtract(changeMoney);
            }
            first = cashHistoryService.check(first.getId());
            second.setEarning(first.getEarning().add(changeMoney));
            // cashHistoryService.updateDomain(second);
        }

        return this.setSuccessResult();
    }

    @Log(title = "修改余额", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改余额")
    @PostMapping("update")
    public ResponseResult update(@RequestBody CashAddDto cash) {

        SysUser user = this.getUser();
        GoogleAuthenticationTool.checkGoogleCode(user.getRandomSecretKey(), cash.getCode());
        Cash domain = this.cashService.check(cash.getId());
        BigDecimal earning = BigDecimal.ZERO;
        String changeType;
        if (cash.getEarning().compareTo(BigDecimal.ZERO) < 0) {
            changeType = "reduce";
            earning = earning.subtract(cash.getEarning());
        } else if (cash.getEarning().compareTo(BigDecimal.ZERO) > 0) {
            changeType = "add";
            earning = cash.getEarning();
        } else {
            throw new BusiException("请输入不为0的数");
        }

        if (StrUtil.isBlank(cash.getRemark())) {
            cash.setRemark("系统修改额度");
        }
        CashHistory userCashHis = new CashHistory(domain.getUserId(), earning, changeType, cash.getRemark(), "sysUpdate");
        userCashHis.setSource("admin");
        cashBizService.updateCash(domain.getUserId(), userCashHis);

        return this.setSuccessResult();
    }

    @Log(title = "导出明细", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出明细")
    @PostMapping("/export")
    public void export(HttpServletResponse response, CashHistory order) {
        this.checkAdmin();
        CheckTimeUtil.checkTime(order.getParams());

        List<CashHistory> list = cashHistoryService.list(order);
        ExcelUtil<CashHistory> util = new ExcelUtil<CashHistory>(CashHistory.class);
        util.exportExcel(response, list, "余额明细数据");
    }


}
